import axios from 'axios'
import elementUI from 'element-ui'

let api = {
  base: '',//网关前缀
  upload_image_url: '',//上传图片URL地址
  upload_file_url: ''//上传文件URL地址
};

if (process.env.NODE_ENV == 'development') {
  api.base = 'http://localhost:8881/';
  // api.upload_image_url = 'http://localhost:9000/upload/image';
  // api.upload_file_url = 'http://localhost:9000/upload/file';
} else if (process.env.NODE_ENV == 'production') {
  api.base = 'https://sz.sqsaipu.com/sz-api/';
  // api.upload_image_url = 'https://sz.sqsaipu.com/sz-api/upload/image';
  // api.upload_file_url = 'https://sz.sqsaipu.com/sz-api/upload/file';
}else{
  api.base = 'http://localhost:8881/';
  // api.upload_image_url = 'http://localhost:9000/upload/image';
  // api.upload_file_url = 'http://localhost:9000/upload/file';
}

api.ajax = axios.create({
  baseURL: api.base,
  timeout: 10000
});

api.ajax.interceptors.request.use(function (config) {
  return config;
}, function (error) {
  return Promise.reject(error);
});

api.ajax.interceptors.response.use(function (response) {
  if (response.status === 200) {
    return response.data;
  } else {
    elementUI.Message.warning('请求出错');
  }
}, function (error) {
  elementUI.Message.warning('服务器异常，1秒后跳转登录页');

  // // 跳转至登录页
  // setTimeout(() => {
  //   location.replace('');
  // }, 1000);
});

export default api;
